<?php
/**
*	WebSocket
*/
namespace Rainrock\Framework\kernel\socket;


use Rainrock\Framework\kernel\base\CLog;
use Rainrock\Framework\kernel\base\Rock;

class WebSocket_Client{
	
	private $client;
	
	
	public function __construct($client)
	{
		$this->client = $client;
		$this->initClient();
	}
	
	
	public function close()
	{
		fclose($this->client);
	}
	
	private function initClient()
	{	
		$data 		= fread($this->client, 1024);
		
		//if(!is_resource($this->client) || feof($this->client)){
			//$this->close();
		//	return;
		//}
		
		$response 	= "Server received:" . $data;
		CLog::show($response);
		
		$this->writeHtml('abchtml:'.Rock::now().'');
		$this->close();
	}
	
	public function writeHtml($msg)
	{
		$body = "HTTP/1.1 200 OK\r\n";
		$body.= "Server: rockrain\r\n";
		$body.= "Connection: keep-alive\r\n";
		$body.= "Content-Type: text/html;charset=utf-8\r\n";
		$body.= "Date: ".time()."\r\n";
		
		$body.= "\r\n".$msg."";
		
		fwrite($this->client, $body);
	}
}